package com.ap.core.business.message.email.handler.impl;

import com.ap.core.business.message.email.handler.EmailHandler;
import com.ap.core.cxf.email.jaxb.managemessage.DataMapInfoType;
import com.ap.core.cxf.email.jaxb.managemessage.SendEmail1Request;
import com.ap.core.cxf.email.jaxb.managemessage.SendEmail1Response;
import com.ap.core.cxf.email.jaxb.managemessage.TemplateNameType;
import com.ap.core.dao.FinancialInstitutionDao;
import com.ap.core.domain.FinancialInstitution;
import com.ap.core.message.manageconsumer.info.RequestRegisterConsumerTypeInfo;
import com.ap.core.util.JaxbUtil;
import com.ap.wsdl.managemessageservice_1.Fault;
import com.ap.wsdl.managemessageservice_1.ManageMessage1PortType;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Implementation class for {@link EmailHandler<RequestRegisterConsumerTypeInfo>}
 */
public class ZappConsumerEmailHandler implements EmailHandler<RequestRegisterConsumerTypeInfo> {

    private FinancialInstitutionDao financialInstitutionDao;
    private ManageMessage1PortType manageMessage1PortType;

    @Override
    @Transactional(readOnly = true)
    public void handleEmailRequest(RequestRegisterConsumerTypeInfo request, String verificationId) {
        final FinancialInstitution financialInstitution = financialInstitutionDao.getFIByIdentification(request.getRegistrationInfo().getFIId());

        final SendEmail1Request sendEmail1Request = new SendEmail1Request();
        sendEmail1Request.setTemplateName(TemplateNameType.WELCOMETOZAPP);

        final List<String> toEmailAddresses = sendEmail1Request.getTo();
        toEmailAddresses.add(request.getRegistrationInfo().getCredentials().getEmailId());


        final List<DataMapInfoType> dataMapInfoTypes = sendEmail1Request.getDataMap();
        DataMapInfoType dataMapInfoType = new DataMapInfoType();
        dataMapInfoType.setName("FI_NAME");
        dataMapInfoType.setValue(financialInstitution.getFiName());
        dataMapInfoTypes.add(dataMapInfoType);

        DataMapInfoType uuid_dataMapInfoType = new DataMapInfoType();
        uuid_dataMapInfoType.setName("UU_ID");
        uuid_dataMapInfoType.setValue(verificationId);

        dataMapInfoTypes.add(uuid_dataMapInfoType);

        DataMapInfoType title_dataMapInfoType = new DataMapInfoType();
        title_dataMapInfoType.setName("title");
        title_dataMapInfoType.setValue("Welcome to Zapp!");

        dataMapInfoTypes.add(title_dataMapInfoType);

        JaxbUtil.newInstance().javaToXML(SendEmail1Request.class, sendEmail1Request);

        try {
            SendEmail1Response sendEmail1Response = manageMessage1PortType.sendEmail1(sendEmail1Request);
        } catch (Fault sendEmail1Fault) {
                throw new RuntimeException("There is problem in sending verification email",sendEmail1Fault.getCause());
        }

    }

    public void setFinancialInstitutionDao(FinancialInstitutionDao financialInstitutionDao) {
        this.financialInstitutionDao = financialInstitutionDao;
    }

    public void setManageMessage1PortType(ManageMessage1PortType manageMessage1PortType) {
        this.manageMessage1PortType = manageMessage1PortType;
    }
}
